package com.lhl.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.lhl.security.domain.User;
import com.lhl.security.utils.JwtUtils;
import com.lhl.security.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 登录接口
 */
@RestController
public class LoginController {

    private final AuthenticationManager authenticationManager;
    private final RedisTemplate<String, String> redisTemplate;

    public LoginController(AuthenticationManager authenticationManager, RedisTemplate<String, String> redisTemplate) {
        this.authenticationManager = authenticationManager;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 登录接口 传入账号密码
     *
     * @param user 用户
     * @return jwt jwt
     */
    @PostMapping("/user/login")
    public R userLogin(@RequestBody User user) {
        Authentication authenticate = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword())
        );
        if (Objects.isNull(authenticate)) {
            return R.no().message("登录失败！");
        }
        user = (User) authenticate.getPrincipal();
        String jwt = JwtUtils.createJWT(user.getId().toString());
        return R.ok().data(jwt);
    }

    /**
     * 微信登录
     *
     * @param user
     * @return
     */
    @PostMapping("/wechat/login")
    public R wechatLogin(@RequestBody User user) {
        Authentication authenticate = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword())
        );
        if (Objects.isNull(authenticate)) {
            return R.no().message("登录失败！");
        }
        user = (User) authenticate.getPrincipal();
        String jwt = JwtUtils.createJWT(user.getId().toString());
        return R.ok().data(jwt);
    }

    /**
     * 注销接口
     *
     * @param request 请求头
     * @return R
     */
    @PostMapping("/loginOut")
    public R loginOut(HttpServletRequest request) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Boolean delete = redisTemplate.opsForValue().getOperations().delete("USER::" + user.getId());
        return Boolean.TRUE.equals(delete) ? R.ok() : R.no();
    }

}
